import { useRouter } from 'vue-router'
import { reactive } from 'vue'
import { v4 as uuidv4 } from 'uuid'
import { regImg, signUp } from '@/apis/user'
interface regFormType {
    username: string
    password: string
    confirmPassword: string
    captcha: string
    captchaSvg: any
}

export function regHook() {
    const router = useRouter()

    const reg = reactive<regFormType>({
        username: '',
        password: '',
        confirmPassword: '',
        captcha: '',
        captchaSvg: null,
    })

    const getRegImg = async () => {
        const { data } = await regImg({ id: uuidv4() })
        reg.captchaSvg = data
    }

    const onSubmit = async () => {
        const { status } = await signUp(reg)
        if (status) router.back()
    }

    const onClickLeft = () => {
        router.back()
    }
    return { reg, onSubmit, getRegImg,onClickLeft }
}
